﻿Ext.define('app.prm.detail.EditProgress',
{
    extend: 'Ext.window.Window',
    border: true,
    title: '编辑状态',
    width: 550,
    modal: true,
    shadow: true,
    autoHeight: true,
    projectId: 0,
    initComponent: function () {
        var me = this;
        // this.addEvents("savesuccess");
        this.items = [this.getForm()];
        this.buttons = [
            {
                text: '保存',
                handler: function () { this.submit(); }, scope: this
            },
            {
                text: '取消',
                handler: function () { this.close(); }, scope: this
            }
        ];
        me.callParent();
    },
    listeners:
    {
        afterrender: function (me)
        {
            App.AjaxGet('/api/services/app/project/GetLastProgressOutPut?projectId=' + me.projectId, function (data)
            {
                me.form.getForm().setValues(data.result);
            });
        }
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.FormPanel(
            {
                border: false,
                frame: false,
                bodyStyle: 'padding:5px',
                labelAlign: 'right',
                defaults: { width: '98%' },
                items: [
                    { xtype: 'hidden', name: 'projectId', value: me.projectId },
                    {
                        xtype: 'container',
                        anchor: '100%',
                        layout: 'hbox',
                        items: [
                            this.getStartTime(),
                            this.getEndTime()
                        ]
                    },
                    this.getSlider(),
                    this.getRadioGroup(),
                   // this.getProgressGroup(),
                    this.getProTask(),
                    this.getTextArea()
                ]
            });
        }
        return this.form;
    },
    getSlider: function () {
        var main = this;
        if (!this.slider) {
            this.slider = new Ext.Slider(
            {
                fieldLabel: '项目进度',
                name: 'progressRate',
                clickToChange: false
                //listeners:
                //{
                //    dragend: function (s, e) {
                //        main.calcEndDate();
                //    }
                //}
            });
        }
        return this.slider;
    },
    getStartTime: function () {
        var main = this;
        if (!this.startTime) {
            this.startTime = new Ext.form.DateField(
                {
                    flex: 1,
                    name: 'actualStartTime',
                    format: 'Y-m-d',
                    value: this.StartTime,
                    fieldLabel: '实际开始'
                }
            );
        }
        return this.startTime;
    },
    getEndTime: function () {
        var main = this;
        console.log('**************');
        console.log(main.statusVal);
        console.log('**************');
        if (!this.endTime) {
            this.endTime = new Ext.form.DateField(
                {
                    flex: 1,
                    name: 'actualEndTime',
                    format: 'Y-m-d',
                    enableKeyEvents: true,
                    value: this.EndTime,
                    fieldLabel: '预期完成'
                }
            );
        }
        return this.endTime;
    },

    getProTask: function () {
        if (!this.taskTextarea) {
            this.taskTextarea = new Ext.form.TextArea({
                height: 130,
                name: 'task',
                fieldLabel: '任务',
                value: this.protask,
                scope: this
            });
        }
        return this.taskTextarea;
    },
    getTextArea: function () {
        if (!this.textarea) {
            this.textarea = new Ext.form.TextArea({
                height: 130,
                name: 'note',
                value: this.processResult,
                fieldLabel: '进展描述'
            });
        }
        return this.textarea;
    },
    getStatus: function (_name) {
        if (!_name)
        { _name = "rado_status"; }
        var radios = this.find("name", _name);
        var status = "";
        for (var i = 0; i < radios.length; i++) {
            var rdo = radios[i];
            var checked = rdo.getValue();
            if (checked) {
                status = rdo.boxLabel;
            }
        }
        return status;
    },
    submit: function () {
        var me = this;
        var formValues = me.form.getForm().getValues();

        var status = me.getRadioGroup().getChecked()[0].boxLabel;
        console.log(status);
        App.AjaxPost('/api/services/app/project/CreateProjectProgress', formValues, function (data) {
            formValues.status = status;
            me.fireEvent("savesuccess", formValues);
        });
    },
    getRadioGroup: function () {
        if (!this.radiogroup) {
            this.radiogroup = new Ext.form.RadioGroup(
            {
                fieldLabel: '项目状态',
                // columns: 5,
                vertical: true,
                items: [
                    { boxLabel: '进行中', name: 'status', inputValue: '10', checked: true },
                    { boxLabel: '完成', name: 'status', inputValue: '20' },
                    { boxLabel: '暂停', name: 'status', inputValue: '30' },
                    { boxLabel: '终止', name: 'status', inputValue: '40' },
                    { boxLabel: '准备', name: 'status', inputValue: '50' }
                ]
            });
        }
        return this.radiogroup;
    },
    getProgressGroup: function () {
        if (!this.progressgroup) {
            this.progressgroup = new Ext.form.RadioGroup(
            {
                fieldLabel: '进度状态',
                // columns: 3,
                vertical: true,
                items: [
                    { boxLabel: '正常', name: 'pg', inputValue: '10', checked: true },
                    { boxLabel: '延期', name: 'pg', inputValue: '20' },
                    { boxLabel: '提前', name: 'pg', inputValue: '30' }
                ]
            });
        }
        return this.progressgroup;
    }
});